package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.QuizReplyGetLikesAction;
import com.jwdf.uda.ui.adapter.ReplyLikeAdapter;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/10/17.
 */
public class ReplyAnswerLikeActivity extends AbsTitleBarActivity{
    private static final String EXTRA_ANSWER_ID="answer_id";

    public static void start(Context context, int answerId) {
        Intent intent = new Intent(context, ReplyAnswerLikeActivity.class);
        intent.putExtra(EXTRA_ANSWER_ID, answerId);
        context.startActivity(intent);
    }

    @Override
    protected String getTopTitle() {
        return getString(R.string.title_answer_likes);
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new LikeListFragment();
    }

    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putInt(EXTRA_ANSWER_ID, getIntent().getIntExtra(EXTRA_ANSWER_ID,0));
        return args;
    }

    public static class LikeListFragment extends SimpleListFragment {
        private ReplyLikeAdapter mAdapter;
        private int mReplyId;
        @Override
        protected void initViews() {
            mReplyId=getArguments().getInt(EXTRA_ANSWER_ID,0);
            mAdapter = new ReplyLikeAdapter(getActivity());
            mDataBinding.commonFragmentList.setAdapter(mAdapter);
        }

        @Override
        protected void onListItemClick(int position) {}
        @Override
        protected void requestData(final boolean refresh) {
            if(refresh && mAdapter.isEmpty()) {
                showProgressDialog();
            }
            int offset = refresh ? 0 : mAdapter.getCount();
            QuizReplyGetLikesAction action = QuizReplyGetLikesAction.newInstance(getActivity(), mReplyId, offset);
            onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResQuestionAnswerLikeList>() {
                @Override
                public void onCallback(int resultCode, Response<PbBean.PbResQuestionAnswerLikeList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                    if (getView() != null) {
                        closeProgressDialog();
                        if (HttpHelper.showError(getActivity(), resultCode, baseServerInfo, R.string.error_operation)) {
                            mDataBinding.commonFragmentList.stopRefresh();
                            mDataBinding.commonFragmentList.stopLoadMore();
                            return;
                        }
                        doLoadLikeListSuccess(refresh,response.body());
                    }
                }
            }));
        }
        private void doLoadLikeListSuccess(boolean refresh, PbBean.PbResQuestionAnswerLikeList body) {
            if(body != null) {
                if(refresh) {
                    mAdapter.setData(body.getLikerInfoList());
                } else {
                    mAdapter.append(body.getLikerInfoList());
                }
                if(body.getLikerInfoList().size() == 0) {
                    mDataBinding.commonFragmentList.setEmptyView(mDataBinding.tvEmptyData);
                }
                mDataBinding.commonFragmentList.setPullLoadEnable(body.getLikerInfoList().size() == getPageLength());
                mDataBinding.commonFragmentList.stopRefresh();
                mDataBinding.commonFragmentList.stopLoadMore();
            }
        }
    }
}
